package com.itmuch.gateway;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

/**
 * @author caiqian
 * @date 2021/5/10 15:21
 */
@Component
@Slf4j
public class TimeBetweenRoutePredicateFactory extends
    AbstractRoutePredicateFactory<TimeBetweenConfig> {

  public TimeBetweenRoutePredicateFactory() {
    super(TimeBetweenConfig.class);
  }

  @Override
  public Predicate<ServerWebExchange> apply(TimeBetweenConfig config) {
    LocalTime start = config.getStart();
    LocalTime end = config.getEnd();

    log.info("start:{}", start);
    log.info("end:{}", end);

    return exchange -> {
      LocalTime now = LocalTime.now();
      log.info("现在时间:{}", now);
      return now.isAfter(start) && now.isBefore(end);
    };
  }

  @Override
  public List<String> shortcutFieldOrder() {
    return Arrays.asList("start", "end");
  }

  public static void main(String[] args) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
    System.out.println(dateTimeFormatter.format(LocalTime.now()));
  }
}
